package com.common.basic.tags;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;

import com.common.basic.base.BaseCommand;
import com.common.basic.base.BaseTaglib;
import com.common.basic.util.Globals;

public class NavigationTaglib extends BaseTaglib 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -357056810745180180L;
	
	private static final String styleEnabled  = "nav_e";
	private static final String styleDisabled = "nav_d";
	
	private String pageIndex;
	private String pageTotal;
	private String formName;
	private String hidden;
	private String action;
	private String name;
	
	
	public int doStartTag() throws JspException 
	{
		super.doStartTag();
		String stName = name!=null?name:"";
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		HttpSession session = request.getSession();
		Locale locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);
		
		BaseCommand baseCommand = (BaseCommand) pageContext.getAttribute(formName, 2);
		int pgIdx = baseCommand.getPgIndex();
		int pgTot = baseCommand.getPgTotal();		
		StringBuffer sb = new StringBuffer("<div>\n");
		
		if(action!=null)
		{
			String pgAction = action.trim();
			if(pgAction.startsWith("${") && pgAction.endsWith("}"))
			{
				sb.append("<form method=\"POST\" action=\"").append(request.getContextPath());
				sb.append(pgAction.substring(2,pgAction.length()-1)).append("\">\n");
			}else{
				sb.append("<form method=\"POST\" action=\"").append(pgAction).append("\">\n");
			}
		}
		
		if(hidden!=null && !"".equals(hidden))
		{
			String[] split = hidden.split(",");
			if(split!=null && split.length>0)
			{
				Object objHidden = null;
				for(int i=0; i<split.length; i++)
				{
					objHidden = request.getParameter(split[i]);
					if(objHidden==null)
						objHidden = request.getAttribute(split[i]);					
					sb.append("<input type=\"hidden\" name=\"").append(split[i]).append("\" ");
					sb.append("value=\"").append(objHidden!=null?String.valueOf(objHidden):"").append("\">\n");  
				}
			}
		}
		
		sb.append("<input type=\"hidden\" name=\"").append(pageTotal).append("\" ");
		sb.append("value=\"").append(pgTot).append("\">\n");
		
		sb.append("<input type=\"submit\" class=\"").append((pgIdx==1 && pgTot==1)?styleDisabled:styleEnabled).append("\" ").append("value=\"");
		sb.append(getMessage("button.go", null, locale)).append("\"");
		sb.append((pgIdx==1 && pgTot==1)?" disabled=\"true\">":">");
		
		if((pgIdx==1 && pgTot==1))
		{
			sb.append("<input type=\"hidden\" name=\"").append(pageIndex).append("\" ");
			sb.append("value=\"").append(pgIdx).append("\">\n");
			sb.append("<input type=\"text\" ");
		}else
			sb.append("<input type=\"text\" name=\"").append(pageIndex).append("\" ");
		
		sb.append("size=\"3\" value=\"").append(pgIdx).append("\" ");
		sb.append("style=\"TEXT-ALIGN: center;\">\n");
		
		sb.append("&nbsp;").append(getMessage("label.of", null, locale));
		sb.append("&nbsp;").append(pgTot).append("&nbsp;&nbsp;\n");
		
		sb.append("<input type=\"button\" class=\"").append(pgIdx==1?styleDisabled:styleEnabled).append("\" ");
		sb.append("value=\"").append(getMessage("button.first", null, locale)).append("\" ");
		sb.append("onclick=\"do").append(stName).append("First()").append("\" ");
		if(pgIdx==1) sb.append("disabled=\"true\" ");
		sb.append(">\n");		
		
		sb.append("<input type=\"button\" class=\"").append(pgIdx==1?styleDisabled:styleEnabled).append("\" ");
		sb.append("value=\"").append(getMessage("button.prev", null, locale)).append("\" ");
		sb.append("onclick=\"do").append(stName).append("Prev()").append("\" ");
		if(pgIdx==1) sb.append("disabled=\"true\" ");
		sb.append(">\n");
				
		sb.append("<input type=\"button\" class=\"").append(pgIdx==pgTot?styleDisabled:styleEnabled).append("\" ");
		sb.append("value=\"").append(getMessage("button.next", null, locale)).append("\" ");
		sb.append("onclick=\"do").append(stName).append("Next()").append("\" ");
		if(pgIdx==pgTot) sb.append("disabled=\"true\" ");
		sb.append(">\n");
		
		sb.append("<input type=\"button\" class=\"").append(pgIdx==pgTot?styleDisabled:styleEnabled).append("\" ");
		sb.append("value=\"").append(getMessage("button.last", null, locale)).append("\" ");
		sb.append("onclick=\"do").append(stName).append("Last()").append("\" ");
		if(pgIdx==pgTot) sb.append("disabled=\"true\" ");
		sb.append(">\n");
		
		sb.append("<script type=\"text/javascript\">").append("\n");
		
		sb.append("function do").append(stName).append("First(){").append("\n");
		sb.append("with(document.getElementById(\"").append(formName).append("\")){").append("\n");
		sb.append(pageIndex).append(".value=1;").append("\n");
		sb.append("submit();").append("\n");
		sb.append("}").append("\n");
		sb.append("}").append("\n");
		
		sb.append("function do").append(stName).append("Prev(){").append("\n");
		sb.append("with(document.getElementById(\"").append(formName).append("\")){").append("\n");
		sb.append(pageIndex).append(".value=eval(").append(pageIndex).append(".value)-1;").append("\n");
		sb.append("submit();").append("\n");
		sb.append("}").append("\n");
		sb.append("}").append("\n");
		
		sb.append("function do").append(stName).append("Next(){").append("\n");
		sb.append("with(document.getElementById(\"").append(formName).append("\")){").append("\n");
		sb.append(pageIndex).append(".value=eval(").append(pageIndex).append(".value)+1;").append("\n");
		sb.append("submit();").append("\n");
		sb.append("}").append("\n");
		sb.append("}").append("\n");
		
		sb.append("function do").append(stName).append("Last(){").append("\n");
		sb.append("with(document.getElementById(\"").append(formName).append("\")){").append("\n");
		sb.append(pageIndex).append(".value=").append(pgTot).append(";").append("\n");
		sb.append("submit();").append("\n");
		sb.append("}").append("\n");
		sb.append("}").append("\n");
		
		sb.append("</script>").append("\n");
		
		if(action!=null) sb.append("</form>\n");
		sb.append("</div>\n");
		
		write(pageContext, sb.toString());
		sb = null;
		return SKIP_BODY;
	}

	public String getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(String pageIndex) {
		this.pageIndex = pageIndex;
	}

	public String getPageTotal() {
		return pageTotal;
	}

	public void setPageTotal(String pageTotal) {
		this.pageTotal = pageTotal;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getHidden() {
		return hidden;
	}

	public void setHidden(String hidden) {
		this.hidden = hidden;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
